package smppload;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelFuture;

import com.cloudhopper.commons.util.LoadBalancedList;
import com.cloudhopper.commons.util.LoadBalancedLists;
import com.cloudhopper.commons.util.RoundRobinLoadBalancedList;
import com.cloudhopper.smpp.SmppBindType;
import com.cloudhopper.smpp.SmppConstants;
import com.cloudhopper.smpp.SmppSession;
import com.cloudhopper.smpp.SmppSessionConfiguration;
import com.cloudhopper.smpp.impl.DefaultSmppSession;
import com.cloudhopper.smpp.pdu.SubmitSm;
import com.cloudhopper.smpp.pdu.SubmitSmResp;
import com.cloudhopper.smpp.type.Address;
import com.cloudhopper.smpp.type.RecoverablePduException;
import com.cloudhopper.smpp.type.SmppChannelException;
import com.cloudhopper.smpp.type.SmppTimeoutException;
import com.cloudhopper.smpp.type.UnrecoverablePduException;
import com.cloudhopper.smpp.util.LongMessageUtil;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author oashafa
 */
public class SMPPLoad extends javax.swing.JFrame {

	private boolean running = false;
	private boolean connected = false;
	private volatile boolean threadRunning = true;
	private LoadBalancedList<OutboundClient> balancedList;
	private static long totalTime = 0;
	private static int count = 0;

	/**
	 * Creates new form Main
	 */
	public SMPPLoad() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		serverField = new javax.swing.JTextField();
		portField = new javax.swing.JTextField();
		threadsField = new javax.swing.JTextField();
		msgsField = new javax.swing.JTextField();
		durationField = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		sysidField = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();
		passwordField = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		logWindow = new javax.swing.JTextArea();
		clear = new javax.swing.JButton();
		jButton1 = new javax.swing.JButton();
		jLabel8 = new javax.swing.JLabel();
		fixedCodeField = new javax.swing.JTextField();
		jButton2 = new javax.swing.JButton();
		jLabel9 = new javax.swing.JLabel();
		destField = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("SMPP Load Driver");

		jLabel1.setText("Server");

		jLabel2.setText("Port");

		jLabel3.setText("Threads");

		jLabel4.setText("Msgs/sec");

		jLabel5.setText("Duration (secs)");

		serverField.setText("0.0.0.0");
		serverField.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				serverFieldActionPerformed(evt);
			}
		});

		portField.setText("2776");
		portField.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				portFieldActionPerformed(evt);
			}
		});

		threadsField.setText("1");

		msgsField.setText("1");

		durationField.setText("10");
		durationField.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				durationFieldActionPerformed(evt);
			}
		});

		jLabel6.setText("Sys ID");

		sysidField.setText("smpp");

		jLabel7.setText("Password");

		passwordField.setText("test1234$");

		logWindow.setEditable(false);
		logWindow.setColumns(20);
		logWindow.setLineWrap(true);
		logWindow.setRows(5);
		logWindow.setToolTipText("");
		jScrollPane1.setViewportView(logWindow);

		clear.setText("Clear Log");
		clear.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				clearActionPerformed(evt);
			}
		});

		jButton1.setText("Start");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jLabel8.setText("Fixed Code");

		fixedCodeField.setText("16175138311");

		jButton2.setText("Connect");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jLabel9.setText("Destination");

		destField.setText("13392965957");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jScrollPane1)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(jLabel1)
																						.addComponent(jLabel2)
																						.addComponent(jLabel3)
																						.addComponent(jLabel4)
																						.addComponent(jLabel5))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								jPanel1Layout
																										.createSequentialGroup()
																										.addGroup(
																												jPanel1Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addGroup(
																																jPanel1Layout
																																		.createParallelGroup(
																																				javax.swing.GroupLayout.Alignment.TRAILING,
																																				false)
																																		.addComponent(
																																				portField,
																																				javax.swing.GroupLayout.Alignment.LEADING)
																																		.addComponent(
																																				serverField,
																																				javax.swing.GroupLayout.Alignment.LEADING,
																																				javax.swing.GroupLayout.DEFAULT_SIZE,
																																				100,
																																				Short.MAX_VALUE))
																														.addComponent(
																																threadsField,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																30,
																																javax.swing.GroupLayout.PREFERRED_SIZE))
																										.addGap(33, 33,
																												33)
																										.addGroup(
																												jPanel1Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addGroup(
																																jPanel1Layout
																																		.createSequentialGroup()
																																		.addComponent(
																																				jLabel6,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				48,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																																		.addComponent(
																																				sysidField,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				100,
																																				javax.swing.GroupLayout.PREFERRED_SIZE))
																														.addGroup(
																																jPanel1Layout
																																		.createParallelGroup(
																																				javax.swing.GroupLayout.Alignment.TRAILING,
																																				false)
																																		.addGroup(
																																				javax.swing.GroupLayout.Alignment.LEADING,
																																				jPanel1Layout
																																						.createSequentialGroup()
																																						.addComponent(
																																								jLabel8)
																																						.addPreferredGap(
																																								javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																						.addComponent(
																																								fixedCodeField))
																																		.addGroup(
																																				jPanel1Layout
																																						.createSequentialGroup()
																																						.addComponent(
																																								jLabel7)
																																						.addPreferredGap(
																																								javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																																						.addComponent(
																																								passwordField,
																																								javax.swing.GroupLayout.PREFERRED_SIZE,
																																								100,
																																								javax.swing.GroupLayout.PREFERRED_SIZE)))))
																						.addGroup(
																								jPanel1Layout
																										.createSequentialGroup()
																										.addGroup(
																												jPanel1Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING,
																																false)
																														.addComponent(
																																durationField,
																																javax.swing.GroupLayout.Alignment.LEADING,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																50,
																																Short.MAX_VALUE)
																														.addComponent(
																																msgsField,
																																javax.swing.GroupLayout.Alignment.LEADING))
																										.addGroup(
																												jPanel1Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addGroup(
																																jPanel1Layout
																																		.createSequentialGroup()
																																		.addGap(83,
																																				83,
																																				83)
																																		.addComponent(
																																				jLabel9)
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addComponent(
																																				destField,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				155,
																																				javax.swing.GroupLayout.PREFERRED_SIZE))
																														.addGroup(
																																jPanel1Layout
																																		.createSequentialGroup()
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																																		.addComponent(
																																				jButton2,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				96,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addComponent(
																																				jButton1,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				74,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addComponent(
																																				clear,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				94,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)))))
																		.addGap(0, 42, Short.MAX_VALUE)))
										.addContainerGap()));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(serverField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel6)
														.addComponent(sysidField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel1))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout

																		.createSequentialGroup()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								portField,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(jLabel2))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								threadsField,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(jLabel8)
																						.addComponent(
																								fixedCodeField,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(jLabel3))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								msgsField,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(jLabel9)
																						.addComponent(
																								destField,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(jLabel4))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								durationField,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(jLabel5)
																						.addComponent(jButton2)
																						.addComponent(jButton1)
																						.addComponent(clear)))
														.addGroup(
																jPanel1Layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																		.addComponent(passwordField,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(jLabel7)))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 236,
												Short.MAX_VALUE).addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 485, Short.MAX_VALUE)
				.addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 391, Short.MAX_VALUE)
				.addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		if (!connected) {
			connect();
		} else {
			disconnect();
		}
	}// GEN-LAST:event_jButton2ActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		try {
			// TODO add your handling code here:
			if (!running) {
				running = true;
				jButton1.setText("Stop");
				startSMPPLoad();
			} else {
				stopSMPPLoad();
			}
		} catch (IOException ex) {
			Logger.getLogger(SMPPLoad.class.getName()).log(Level.SEVERE, null, ex);
		} catch (RecoverablePduException ex) {
			Logger.getLogger(SMPPLoad.class.getName()).log(Level.SEVERE, null, ex);
		} catch (InterruptedException ex) {
			Logger.getLogger(SMPPLoad.class.getName()).log(Level.SEVERE, null, ex);
		} catch (SmppChannelException ex) {
			Logger.getLogger(SMPPLoad.class.getName()).log(Level.SEVERE, null, ex);
		} catch (UnrecoverablePduException ex) {
			Logger.getLogger(SMPPLoad.class.getName()).log(Level.SEVERE, null, ex);
		} catch (SmppTimeoutException ex) {
			Logger.getLogger(SMPPLoad.class.getName()).log(Level.SEVERE, null, ex);
		}
	}// GEN-LAST:event_jButton1ActionPerformed

	private void clearActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_clearActionPerformed
		logWindow.setText(null);
	}// GEN-LAST:event_clearActionPerformed

	private void durationFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_durationFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_durationFieldActionPerformed

	private void portFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_portFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_portFieldActionPerformed

	private void serverFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_serverFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_serverFieldActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(SMPPLoad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(SMPPLoad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(SMPPLoad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(SMPPLoad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new SMPPLoad().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton clear;
	private static javax.swing.JTextField destField;
	private static javax.swing.JTextField durationField; // 持续发送次数
	private javax.swing.JTextField fixedCodeField;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private static javax.swing.JTextArea logWindow;
	private static javax.swing.JTextField msgsField; // 短信内容
	private static javax.swing.JTextField passwordField; // 密码
	private static javax.swing.JTextField portField; // 端口
	private static javax.swing.JTextField serverField;
	private static javax.swing.JTextField sysidField; // 系统Id
	private static javax.swing.JTextField threadsField; // 线程的数量

	// End of variables declaration//GEN-END:variables

	/**
	 * 开始链接
	 * 
	 */
	private void connect() {
		final int threadCount = Integer.parseInt(threadsField.getText());

		DummySmppClientMessageService smppClientMessageService = new DummySmppClientMessageService();
		balancedList = LoadBalancedLists.synchronizedList(new RoundRobinLoadBalancedList<OutboundClient>());

		for (int i = 0; i < threadCount; ++i) {
			balancedList.set(createClient(smppClientMessageService, ++i), 1);
		}

		connected = true;
		jButton2.setText("Disconnect");
	}

	/**
	 * 断开连接
	 */
	private void disconnect() {
		for (LoadBalancedList.Node<OutboundClient> node : balancedList.getValues()) {
			node.getValue().shutdown();
		}
		connected = false;
		jButton2.setText("Connect");
	}

	/**
	 * start the SMPP Load in a thread
	 */
	private void startSMPPLoad() throws IOException, RecoverablePduException, InterruptedException,
			SmppChannelException, UnrecoverablePduException, SmppTimeoutException {
		// final long msgspersec = 1000/Long.parseLong(msgsField.getText());
		final int countdown = Integer.parseInt(durationField.getText()) * Integer.parseInt(msgsField.getText());
		final String fixedCode = fixedCodeField.getText();
		threadRunning = true;

		Thread thread = new Thread(new Runnable() {

			// private volatile boolean running = true;
			final AtomicLong alreadySent = new AtomicLong();

			@Override
			public void run() {

				while (threadRunning) {
					try {

						final OutboundClient client = balancedList.getNext();
						final SmppSession session = client.getSession();
						if ((session != null) && session.isBound()) {
							// String text =
							// "【东莞市人力资源局】通知：解`密对方的s电-话卡，可接收s对-方的asd【东莞市人力资源局】通知：解`密对方的电-话卡，可接收对-方的一切往来短`信及通`话内容。咨询13121855033王 ,尊敬的王鑫，您4月20日在中国银行成功定投嘉实沪深300ETF联接(LOF)81.16份，当日净值1.2174";
							// byte[] textBytes = CharsetUtil.encode(text,CharsetUtil.NAME_UTF_8); //测试长短信
							String text = "上周,国务院常务会议明确提出电信企业要“推出流量不清零、流量转增等服务”,随后三大运营商出台的资费新方案也都涉及了“不清零和流量转增”等内容,但仍有网友质疑:为什么固定套餐内的流量还要清零?还是降低套餐资费靠谱";
							int msgLength = text.getBytes("UnicodeBigUnmarked").length;
							if (msgLength > 140) {

								List<byte[]> smsList = LongMessageUtil.splitLongMessage(text, "UnicodeBigUnmarked",
										140, 6);
								logWindow.append("长短信分解的条数：" + smsList.size() + "\n");
								long sent = alreadySent.incrementAndGet();
								for (byte[] item : smsList) {
									SubmitSm submit = new SubmitSm();
									submit.setSourceAddress(new Address((byte) 0x03, (byte) 0x00, fixedCode));
									String address = getNewAddress();
									submit.setDestAddress(new Address((byte) 0x01, (byte) 0x01, address));
									submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED);
									submit.setShortMessage(item);

									logWindow.append("Sending message " + sent + " to " + address + "\n");

									long startTime = System.currentTimeMillis();
									// final SubmitSmResp submit1 = session.submit(submit, 30000);// 30000毫秒内需要有response

									// 检查是否分配过序号
									if (!submit.hasSequenceNumberAssigned()) {
										submit.setSequenceNumber(((DefaultSmppSession) session).getSequenceNumber()
												.next());
									}

									// 编码消息
									ChannelBuffer buffer = ((DefaultSmppSession) session).transcoder.encode(submit);
									ChannelFuture channelFuture = ((DefaultSmppSession) session).channel.write(buffer)
											.await();

									long endTime = System.currentTimeMillis();
									long respTime = endTime - startTime;
									averageTime(respTime);
									logWindow.append("Response time: " + respTime + "ms\n");
									if (sent == countdown) {
										threadRunning = false;
										stopSMPPLoad();
									}
								}
							} else {
								long sent = alreadySent.incrementAndGet();
								SubmitSm submit = new SubmitSm();
								submit.setSourceAddress(new Address((byte) 0x03, (byte) 0x00, fixedCode));
								String address = getNewAddress();
								submit.setDestAddress(new Address((byte) 0x01, (byte) 0x01, address));
								submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED);
								submit.setShortMessage(text.getBytes("UnicodeBigUnmarked"));

								logWindow.append("Sending message " + sent + " to " + address + "\n");

								long startTime = System.currentTimeMillis();
								final SubmitSmResp submit1 = session.submit(submit, 30000);
								long endTime = System.currentTimeMillis();
								long respTime = endTime - startTime;
								averageTime(respTime);
								logWindow.append("Response time: " + respTime + "ms\n");

								if (sent == countdown) {
									threadRunning = false;
									stopSMPPLoad();
								}
							}
							// Thread.sleep(msgspersec);
						}
					} catch (Exception e) {
						System.err.println(e.toString());
					}
				}
			}
		});
		thread.start();
	}

	/**
	 * Generate a new desitnation MDN based on the destination field. '*'
	 * character is a wildcard that can be [0-9]
	 */
	private static String getNewAddress() {
		String dest = destField.getText();
		/*
		 * int c = dest.length() - dest.replace("*", "").length(); String start
		 * = dest.replace("*",""); if (c > 0){ Random r = new Random(); char[]
		 * digits = new char[c]; digits[0] = (char) (r.nextInt(9) + '1'); for
		 * (int i = 1; i < c; i++) { digits[i] = (char) (r.nextInt(10) + '0'); }
		 * return start + new String(digits); } else { return start; }
		 */
		return dest;
	}

	/**
	 * Stop the smpp load and display average response time
	 */
	private void stopSMPPLoad() {
		threadRunning = false;
		running = false;
		jButton1.setText("Start");
		logWindow.append("Average response time: " + (totalTime / count) + "ms\n");
	}

	/**
	 * Keep track of the average time for each message sent
	 */
	private static void averageTime(long time) {
		totalTime = totalTime + time;
		count++;
	}

	private OutboundClient createClient(DummySmppClientMessageService smppClientMessageService, int i) {
		OutboundClient client = new OutboundClient(logWindow);
		client.initialize(getSmppSessionConfiguration(i), smppClientMessageService);
		client.scheduleReconnect();
		return client;
	}

	private SmppSessionConfiguration getSmppSessionConfiguration(int i) {

		SmppSessionConfiguration config = new SmppSessionConfiguration();
		config.setWindowSize(5);
		config.setName("LoadTester.Session." + i);
		config.setType(SmppBindType.TRANSCEIVER);
		config.setHost(serverField.getText());
		config.setPort(Integer.parseInt(portField.getText()));
		config.setConnectTimeout(10000);
		config.setSystemId(sysidField.getText());
		config.setPassword(passwordField.getText());
		config.getLoggingOptions().setLogBytes(false);
		// to enable monitoring (request expiration)
		config.setRequestExpiryTimeout(30000);
		config.setWindowMonitorInterval(15000);
		config.setCountersEnabled(false);
		return config;
	}
}
